
import {login,register,UserInfo} from '../../api/user'
import {getItem,setItem} from '../../utils/storage'
import {TOKEN,USERINFO} from "../../constant"


export default{
  state: {
    token : getItem(TOKEN) || "",
    userInfo : getItem(USERINFO) || "",
  },
  mutations: {
    saveToken(state,token){
      state.token = token;
      setItem(TOKEN,token)
    },
    setUserInfo(state,userInfo){
      state.userInfo = userInfo
      setItem(USERINFO,userInfo)
    }

  },
  actions: {
    // 登录
    loginwell({commit},loginForm){
      return new Promise((resolve,reject)=>{
        login(loginForm.username,loginForm.password).then((res)=>{
          console.log(res);
          if(res.status == 200){
            let token = res.data.data.token
            commit("saveToken",token)
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    //注册
    getReg(loginFrom){
      return new Promise((resolve,reject) => {
        register(loginFrom.username,loginFrom.password,loginFrom.repassword).then(res => {
          resolve(res)
        })
      })
    },
    //获取个人信息
    async getUserInfo({commit}){
      const res = await UserInfo()
      console.log(res);
      commit("setUserInfo",res)
      return res
    }
  },
  modules: {
  }
}
